package tcm.test.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tcm.test.pojo.dto.OrderDTO;
import tcm.test.pojo.entity.Order;
import tcm.test.pojo.entity.result;
import tcm.test.service.OrderService;

@Slf4j
@RestController
@RequestMapping("/orders")

public class OrderController {

    @Autowired
    private OrderService OrderService;

    //创建新订单
    @PostMapping
    private result<String> register(@RequestBody OrderDTO orderDTO) {
        log.info("注册订单 : {}", orderDTO);
        OrderService.register(orderDTO);
        return result.success();
    }

    //获取指定 ID 订单的详情
    @GetMapping("/{id}")
    private result<Order> getById(@PathVariable Long id) {
        log.info("获取指定 ID 订单的详情，{}", id);
        Order o = OrderService.getById(id);
        return result.success(o);
    }

    //更新指定 ID 订单的详情
    @PutMapping("/{id}")
    private result<String> update(@PathVariable Long id, @RequestBody OrderDTO orderDTO) {
        log.info("更新指定 ID 订单的详情，{}，{}", id,orderDTO);
        orderDTO.setId(id);
        OrderService.update(orderDTO);
        return result.success();
    }

    //删除指定id订单
    @DeleteMapping("/{id}")
    private result<String> delete(@PathVariable Long id) {
        log.info("删除指定id订单");
        OrderService.delete(id);
        return result.success();
    }
}
